package com.ihy.controller;

import com.ihy.entity.Mail;
import com.ihy.service.ServiceMail;
import com.ihy.vo.ResultResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSendException;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
@Slf4j
/*
 * 邮件发送
 */
public class EmailController {
    @Autowired
    public ServiceMail serviceMail;
    @Autowired
    public ResultResponse resultResponse;

    /**
     * 邮件发送
     */
    @RequestMapping("send")
    public ResultResponse sendSimpleMail(Mail mail, String state) {
        if (state != null) {
            try {
                switch (state) {
                    case "text":
                        serviceMail.sendSimpleMail(mail);
                        break;
                    case "templates/html":
                        serviceMail.sendHtmlMail(mail);
                        break;
                    case "attachments":
                        serviceMail.sendAttachmentsMail(mail);
                        break;
                }
            } catch (MailSendException e) {
                resultResponse.setCode(500);
                resultResponse.setMessage("发送失败");
                resultResponse.setData(e.getMessage());
                return resultResponse;
            }
        } else {
            resultResponse.setCode(500);
            resultResponse.setMessage("发送失败");
            resultResponse.setData("参数不可为空");
            return resultResponse;
        }
        resultResponse.setCode(200);
        resultResponse.setMessage("发送成功");
        resultResponse.setData(null);
        return resultResponse;
    }
}